const express = require('express');
const router = express.Router();
const { getAllProducts, getProductById, getCategories } = require('../models/productModel');

// 获取所有商品
router.get('/', async (req, res) => {
    try {
        const { category, minPrice, maxPrice } = req.query;
        const products = await getAllProducts(category, minPrice, maxPrice);
        res.json(products);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// 获取商品详情
router.get('/:id', async (req, res) => {
    try {
        const product = await getProductById(req.params.id);
        if (!product) {
            return res.status(404).json({ error: '商品不存在' });
        }
        res.json(product);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// 获取商品分类
router.get('/categories', async (req, res) => {
    try {
        const categories = await getCategories();
        res.json(categories);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

module.exports = router; 